package com.mew.chess.core.game.pokdeng.game.task;

import com.mew.chess.core.common.handler.ChessPushMessageUtil;
import com.mew.chess.core.game.pokdeng.game.PokDengGameManager;
import com.mew.chess.core.game.pokdeng.game.PokDengSeatManager;
import com.mew.chess.core.game.pokdeng.game.PokDengTableManager;
import com.mew.chess.core.game.pokdeng.game.common.PokDengConstantValue;
import com.mew.chess.core.game.pokdeng.game.common.PokDengLinkType;
import com.mew.chess.core.game.pokdeng.game.common.PokDengPushCMDMapType;
import com.mew.chess.core.game.pokdeng.game.message.CardMessage;
import com.mew.chess.core.game.pokdeng.game.message.PushOpenCardMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dylin
 * @create 2021/4/29
 */
@Component
@Slf4j
public class PokDengOpenCardTask {
    public void openCard(int roomId,int tableId){
        PokDengTableManager table = PokDengGameManager.getInstance().getController(roomId).getTableManager(tableId);
        PushOpenCardMessage message=table.getPushOpenCardMessage();
        message.setBanker(CardMessage.buildByUser(table.getBanker()));
        List<CardMessage> cards=new ArrayList<>();
        for (PokDengSeatManager seat : table.getSeatMap().values()) {
            if(seat.getUser()!=null&&seat.getUser().getCards().size()>0){
                cards.add(CardMessage.buildByUser(seat));
            }
        }
        message.setCards(cards);
        table.changeLink(PokDengLinkType.OpenCard.code(), PokDengConstantValue.OPEN_CARD_TIME);
        ChessPushMessageUtil.broadcastMessage(table.getOnlineUser().values(), PokDengPushCMDMapType.OpenCardMessage.code(),message);
    }
}
